package ca.etsmtl.gti780.lab2;



import java.io.File;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;



import org.apache.http.client.HttpClient;

import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;


import org.apache.http.util.EntityUtils;

import android.app.Activity;

import android.os.Bundle;
import android.widget.TextView;

public class AndroidProjectActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView tv = new TextView(this);
        File file = new File("/sdcard/code-barre-nourriture.jpg");
        try {
                 System.out.println("/sdcard/code-barre-nourriture.jpg");
                 HttpClient client = new DefaultHttpClient();  
                 String postURL = "http://10.192.170.61:8080/CodeServer/DecodePicture";                 
                 HttpPost post = new HttpPost(postURL); 


             FileBody bin = new FileBody(file);
             MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);  
             reqEntity.addPart("picture", bin);
             post.setEntity(reqEntity);  
             HttpResponse response = client.execute(post);  
             HttpEntity resEntity = response.getEntity();  
             if (resEntity != null) {    
                       //Log.i("RESPONSE",EntityUtils.toString(resEntity));
            	 		tv.setText(EntityUtils.toString(resEntity));
                 }
        } catch (Exception e) {
        	tv.setText("exception");
            e.printStackTrace();
        }
        setContentView(tv);
    }
}